package oracle.apps.financials.collection.dao;

import java.sql.SQLException;

import oracle.apps.financials.collection.po.Collector;
import oracle.apps.financials.collection.po.Customer;
import oracle.apps.financials.collection.po.MetricsItem;
import oracle.apps.financials.collection.po.MetricsRaw;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

/**
 * Helper class which creates/updates our database and provides the DAOs.
 * 
 * @author Lucas xu
 */
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

	private static final String DATABASE_NAME = "Collection.db";
	private static final int DATABASE_VERSION = 4;
	private final String LOG_NAME = getClass().getName();

	private Dao<Customer, Integer> customerDao;
	private Dao<Collector, Integer> collectorDao;
	private Dao<MetricsItem, Integer> metricsItemDao;
	private Dao<MetricsRaw, Integer> metricsRawDao;

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, Customer.class);
			TableUtils.createTable(connectionSource, Collector.class);
			TableUtils.createTable(connectionSource, MetricsItem.class);
			TableUtils.createTable(connectionSource, MetricsRaw.class);
		} catch (SQLException e) {
			Log.e(LOG_NAME, "Could not create new table", e);
		}
	}
	
	public void onUpgrade(){	
		this.onUpgrade(this.getWritableDatabase(), this.connectionSource, 3, DATABASE_VERSION);
	}

	@Override
	public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int oldVersion, int newVersion) {
		try {
			TableUtils.dropTable(connectionSource, Customer.class, true);
			TableUtils.dropTable(connectionSource, Collector.class, true);
			TableUtils.dropTable(connectionSource, MetricsItem.class, true);
			TableUtils.dropTable(connectionSource, MetricsRaw.class, true);
			onCreate(sqLiteDatabase, connectionSource);
		} catch (SQLException e) {
			Log.e(LOG_NAME, "Could not upgrade the table", e);
		}
	}

	public Dao<Customer, Integer> getCustomerDao() throws SQLException {
		if (this.customerDao == null) {
			customerDao = getDao(Customer.class);
		}
		return customerDao;
	}

	public Dao<Collector, Integer> getCollectorDao() throws SQLException {
		if (this.collectorDao == null) {
			collectorDao = getDao(Collector.class);
		}
		return collectorDao;
	}

	public Dao<MetricsItem, Integer> getMetricsItemDao() throws SQLException {
		if (metricsItemDao == null) {
			metricsItemDao = getDao(MetricsItem.class);
		}
		return metricsItemDao;
	}

	public Dao<MetricsRaw, Integer> getMetricsRawDao() throws SQLException {
		if (metricsRawDao == null) {
			metricsRawDao = getDao(MetricsRaw.class);
		}
		return metricsRawDao;
	}

}
